package com.linux.huhx.thread;

import java.util.concurrent.*;

public class FutureTest {
    public static void main(String[] args) throws TimeoutException {
        FutureTask<String> future = new FutureTask<String>(new Task());
        new Thread(future).start();
        try {
            System.out.println(future.get(4L, TimeUnit.SECONDS));
            System.out.println("main thread");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    static class Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("begin: " + Thread.currentThread().getName() + ", " + System.currentTimeMillis());
            TimeUnit.SECONDS.sleep(5);
            return "hello";
        }
    }
}

// hello
// main thread